package com.example.administrator.danpin;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Window;

import com.example.administrator.danpin.viewpager.ViewPagerAdapter;
import com.example.administrator.danpin.viewpager.fragments.allgoods.AllGoodsFragment;
import com.example.administrator.danpin.viewpager.fragments.buyer.BuyerFragment;
import com.example.administrator.danpin.viewpager.fragments.editor.EditorFragment;
import com.example.administrator.danpin.viewpager.fragments.find.FindFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //控件
    private ViewPager viewPager;
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private TabLayout tabLayout;

    //viewpager的数据源
    private List<Fragment> data;
    private List<String> titles;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //控件初始化
        initView();

        //初始化数据源
        data=new ArrayList<>();
        titles=new ArrayList<>();
        titles.add("找单品");
        titles.add("编辑推");
        titles.add("买家秀");
        titles.add("全品类");
        data.add(new FindFragment());
        data.add(new EditorFragment());
        data.add(new BuyerFragment());
        data.add(new AllGoodsFragment());


        //viewpageradapter初始化
        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager(),data,titles);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        //设置toolbar到actionbar的位置
        setSupportActionBar(toolbar);

        ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

    }

    private void initView() {
        toolbar= ((Toolbar) findViewById(R.id.activity_main_toolBar));
        viewPager= ((ViewPager) findViewById(R.id.activity_main_viewPager));
        drawerLayout= ((DrawerLayout) findViewById(R.id.activity_main));
        tabLayout= (TabLayout) findViewById(R.id.activity_main_tabLayout);
    }
}
